<?php
declare(strict_types=1);
namespace app\shop\controller;

use app\shop\library\ShopAuth;
use app\shop\logic\PlayLogic;
use app\shop\model\play\user;
use think\response\Json;
use app\shop\exception\ParamsErrorException;

class PlayController extends BaseController
{
    protected array $noNeedLogin = ['getPageList','getTypeList','detail'];
    public function __construct()
    {
        parent::__construct();

        $this->logic = new PlayLogic();
    }

    /**
     * 投票列表
     * @return Json
     * @throws \app\shop\exception\ParamsErrorException
     */
    public function getPageList(): Json
    {
        $params = $this->postInput(true);

        $this->validateData($params, 'list');

        $result = $this->logic->PlayList($params);

        return $this->success($result);
    }

    /**
     * 投票详情
     * @return Json
     * @throws \app\shop\exception\FailException
     * @throws \app\shop\exception\ParamsErrorException
     */
    public function detail(): Json
    {
        $params = $this->postInput(true);

        $result = $this->logic->detail($params);

        return $this->success($result);
    }


    /**
     * 去投票
     * @return \Psr\Http\Message\StreamInterface|Json
     */
    public function add(){
        $params = $this->postInput(true);
        $user = ShopAuth::instance()->getUser();
        $uid = $user->id;
        if(!isset($params['name'])){
            throw new ParamsErrorException('缺少姓名');
        }
        if(!isset($params['id_no'])){
            throw new ParamsErrorException('身份证');
        }
        if(!isset($params['mobile'])){
            throw new ParamsErrorException('联系电话');
        }
        $model = new user();
        if($model->where(['shop_user_id'=>$uid])->find()){
            throw new ParamsErrorException('已申请');
        }
        $model->shop_user_id = $uid;
        $model->name = $params['name'];
        $model->id_no = $params['id_no'];
        $model->mobile = $params['mobile'];
        $model->sex = $params['sex'];
        $model->desc = $params['desc'];
        $model->create_time = time();
        $res = $model->save();
        if(!$res){
            throw new ParamsErrorException('添加失败');
        }
        return $this->success('成功');
    }



}